package com.hicorp.segment.controller;

import com.hicorp.segment.pojo.po.MaterialConcrete;
import com.hicorp.segment.pojo.po.OrderReceiptSubMaterial;
import com.hicorp.segment.pojo.po.Receipts;
import com.hicorp.segment.service.ReceiptsService;
import com.hicorp.segment.utils.ResultBean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/receipts/{tableName}")
public class ReceiptsController {
    private final ReceiptsService receiptsService;

    public ReceiptsController(ReceiptsService receiptsService) {
        this.receiptsService = receiptsService;
    }

    // 查询
    @GetMapping
    public ResultBean<List<Receipts>> selectAll(@PathVariable String tableName) {
        return receiptsService.queryInfo(tableName);
    }

    // 新增
    @PostMapping
    public Object createRecord(@PathVariable String tableName, @RequestBody Receipts receipts) {
        System.out.println(receipts);
        receipts.setTableName(tableName);
//        System.out.println(receipts);
        return receiptsService.insert(receipts);
    }

    // 修改
    @PutMapping("/{id}")
    public Object updateRecord(@PathVariable String tableName, @PathVariable Long id, @RequestBody Receipts receipts) {
//        System.out.println(receipts);
        receipts.setTableName(tableName);
        return receiptsService.update(receipts);
    }

    // 批量删除
    @PostMapping("/batchDelete")
    public Object batchDelete(@PathVariable String tableName, @RequestBody List<Long> ids) {
        return receiptsService.batchDeleteRecord(tableName, ids);
    }

    // 批量作废
    @PostMapping("/batchInvalid")
    public Object batchInvalid(@PathVariable String tableName, @RequestBody List<Long> ids) {
        return receiptsService.batchInvalidRecord(tableName, ids);
    }

    // 组合搜索
    @PostMapping("/search")
    public Object searchRecord(@PathVariable String tableName, @RequestBody Receipts receipts) {
        receipts.setTableName(tableName);
//        System.out.println(receipts);
        return receiptsService.searchRecord(tableName, receipts);
    }

    // 提交审批
    @PostMapping("/submitApprove/{approveStatus}")
    public Object submitApproveRecord(@PathVariable String tableName, @PathVariable String approveStatus, @RequestBody Receipts receipts) {
        return receiptsService.submitApproveRecord(tableName, approveStatus, receipts);
    }

    // 审批
    @PostMapping("/approve/{approveStatus}/{remark}/{user}/{role}")
    public Object approveRecord(@PathVariable String tableName, @PathVariable String approveStatus, @PathVariable String remark, @PathVariable String user, @PathVariable String role, @RequestBody Receipts receipts) {
        System.out.println(approveStatus);
        System.out.println(remark);

        System.out.println(user);

        System.out.println(role);

        return receiptsService.approveRecord(tableName, approveStatus, remark, user, role, receipts);
    }

    // 搜索
    @GetMapping("/{field}/{content}")
    public Object selectRecord(@PathVariable String tableName, @PathVariable String field, @PathVariable String content) {
        return receiptsService.selectRecords(tableName, field, content);
    }

    // 生成单据内部编码
    @GetMapping("/generateReceiptsNo/{preNo}")
    public ResultBean<String> generateReceiptsNo(@PathVariable String tableName, @PathVariable String preNo) {
        System.out.println(tableName);
        System.out.println(preNo);

        return receiptsService.generateReceiptsNo(tableName, preNo);
    }

    // 查询，通过表名和内部编码来获取主子表数据
    @GetMapping("/getReceiptByNo/{receiptNo}")
    public ResultBean<Receipts> selectByTableNameAndNo(@PathVariable String tableName, @PathVariable String receiptNo) {
        return receiptsService.selectByTableNameAndNo(tableName, receiptNo);
    }

    // 远程搜索数据获取，通过表名
    @GetMapping("/autocompleteInput/material")
    public ResultBean<List<MaterialConcrete>> selectAutocompleteInput(@PathVariable String tableName) {
        return receiptsService.selectAutocompleteInput(tableName);
    }

    // 通过no和itemNo来查找订单单据物料BOM表中的数据
    @GetMapping("/orderReceiptSubMaterial/{no}/{itemNo}")
    public ResultBean<List<OrderReceiptSubMaterial>> selectOrderReceiptsSubMaterial(@PathVariable String tableName, @PathVariable String no, @PathVariable String itemNo) {
        return receiptsService.selectOrderReceiptsSubMaterial(tableName, no, itemNo);
    }

    //子组件展开细节提交保存
    @PostMapping("/orderReceiptSubMaterial/submit/{no}/{itemNo}")
    public ResultBean<Integer> submitOrderMaterial(@PathVariable String tableName,@PathVariable String no, @PathVariable String itemNo,@RequestBody ArrayList<OrderReceiptSubMaterial> orderReceiptSubMaterialList) {
        return receiptsService.submitOrderMaterial(tableName,orderReceiptSubMaterialList,no, itemNo);
    }
}
